package com.taotao.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.EasyUIResult;
import com.taotao.common.HttpClientUtil;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbContentExample.Criteria;
import com.taotao.service.ContentService;
@Service
public class ContentServiceImpl implements ContentService {
	@Autowired
	private TbContentMapper contentMapper;
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_CONTENT_CACHE_SYCN}")
	private String REST_CONTENT_CACHE_SYCN;
	
	@Override
	public TaotaoResult insertContent(TbContent content) {
		content.setCreated(new Date());
		content.setUpdated(new Date());
		contentMapper.insert(content);
		//添加缓存同步逻辑
		try {
			HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_CACHE_SYCN+content.getCategoryId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok();
	}

	@Override
	public EasyUIResult getContentList(Integer page, Integer rows,Long parentId) {
		TbContentExample contentExample=new TbContentExample();
		PageHelper.startPage(page, rows);
		Criteria criteria = contentExample.createCriteria();
		criteria.andCategoryIdEqualTo(parentId);
		List<TbContent> contents = contentMapper.selectByExample(contentExample);
		PageInfo<TbContent> pageInfo=new PageInfo<>(contents);
		Long total=pageInfo.getTotal();
		EasyUIResult eui=new EasyUIResult();
		eui.setRows(contents);
		eui.setTotal(total);
		return eui;
	}

}
